package xdu.lz.stage2_design_pattern.chapter9_threadlocal;

import java.util.HashMap;
import java.util.Map;

public class ThreadLocalSimulation<T> {
   private final Map<Thread,T> threadLocal = new HashMap<>();

   public void set(T t){
       synchronized (this){
           Thread key = Thread.currentThread();
           threadLocal.put(key,t);
       }
   }
   public T get(){
       synchronized (this){
           T value = threadLocal.get(Thread.currentThread());
           if(value == null){
               return initialValue();
           }
           return value;
       }
   }

    //可以被子类重写
    protected T initialValue() {
       return null;
    }
}
